--
-- Author: ping.wang
-- Date: 2018-09-04 20:47:34
--
-- ElevatePeerageRender
--

local ClassRef = app.mvc.createRenderClass(...)
local string_format = string.format
--data =
function ClassRef.createWithData(data, node)
    local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.NewGuildElevateRender))
    if node == nil then
        ccui.Helper:doLayout(sprite)
    end
    sprite:init(data)
    return sprite
end
function ClassRef.create(node)
    return ClassRef.createWithData(nil, node)
end

--
function ClassRef:init(data)
    self.super.init(self, data)
    self:setContentSize( self._ui.Panel:getContentSize() )
    self._nomalBg = self._ui.bg_1 --普通
    self._chooseBg = self._ui.bg_choose --选中用这个
    self._qz3 = self._ui.qz_3 --灰色 未解锁
    self._qz2 = self._ui.qz_2 --红色 选中
    self._qz1 = self._ui.qz_1 --蓝色 普通解锁
    --self._icon = self._ui.icon --图标要变更

    self._nomalBg:setVisible(true)
    self._chooseBg:setVisible(false)
    self._qz1:setVisible(true)
    self._qz2:setVisible(false)


    self._ui.Panel:addTouchEventListener(function( sender, eventType )
        if eventType == ccui.TouchEventType.ended then
            if self._data and self._data[3] > 0 then
                if not self._selected then
                    executeFunc(self._onClick, self._data)
                end
            end
        end
    end)
    self:refresh()
end

function ClassRef:refresh()
    if self._data then
        self:setVisible( true )

        local data = self._data
        local titleInfo = data[1]
        self._ui.name:setString(L(titleInfo.name))

        self._ui.icon:loadTexture(string_format("%s%s%d_n.png",Res.guildPositionIconPath,"emperor_icon_",tonumber(titleInfo.ID)))
        if data[3] > 0 then
            self._ui.tips:setString(data[2].."/"..data[3])
            self._qz1:setVisible(true)
            self._qz2:setVisible(false)
            self._qz3:setVisible(false)
            self._ui.name:setOpacity(255)
        else
            self._qz1:setVisible(false)
            self._qz2:setVisible(false)
            self._qz3:setVisible(true)
            self._ui.tips:setString(L(titleInfo.lockDesc))
            --没解锁的文字需要虚化
            self._ui.name:setOpacity(128)
        end
    else
        self:setVisible( false )
    end
end

function ClassRef:setSelected(bl, playerTitleId)
    --只有解锁的可以设置选中和非选中
    if self._data[3] > 0 then
        self._selected = bl
        local titleInfo = self._data[1]
        if bl then
            self._nomalBg:setVisible(false)
            self._chooseBg:setVisible(true)
            self._qz1:setVisible(false)
            self._qz2:setVisible(true)
        else
            self._nomalBg:setVisible(true)
            self._chooseBg:setVisible(false)
            self._qz1:setVisible(true)
            self._qz2:setVisible(false)
        end

        if tonumber(playerTitleId) == tonumber(titleInfo.ID) then
            self._ui.icon:loadTexture(string_format("%s%s%d_p.png",Res.guildPositionIconPath,"emperor_icon_",tonumber(titleInfo.ID)))
        else
            self._ui.icon:loadTexture(string_format("%s%s%d_n.png",Res.guildPositionIconPath,"emperor_icon_",tonumber(titleInfo.ID)))
        end

    end
end

function ClassRef:onBtnClick(cb)
    self._onClick = cb
end

function ClassRef:playAni()
    local aniAppoint = self._ui.ani_appoint
    aniAppoint:setVisible(true)
    aniAppoint:getTimeline():gotoFrameAndPlay( 0, false )

    local actions = {}
    table.insert( actions, cc.DelayTime:create( aniAppoint:getTimeline():getDuration()/60 ) )
    table.insert( actions, cc.CallFunc:create( function() aniAppoint:setVisible(false) end ) )
    aniAppoint:runAction( cc.Sequence:create( actions ) )
end

return ClassRef
